package com.wk.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.springboot.entity.LocalMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.sql.Date;
import java.util.List;

public interface LocalMessageMapper extends BaseMapper<LocalMessage> {
    
    // 待处理消息查询（含分页）
    @Select("SELECT * FROM local_message WHERE status in (0,3) AND next_retry_time <= now()")
    List<LocalMessage> selectPendingMessages(Page<LocalMessage> page);

    // 乐观锁更新重试信息
    @Update("UPDATE local_message SET " +
            "retry_count = #{retryCount}, " +
            "next_retry_time = #{nextRetryTime}, " +
            "version = version + 1 " +
            "WHERE id = #{id} AND version = #{version}")
    int updateRetryInfo(@Param("id") Long id,
                       @Param("retryCount") Integer retryCount,
                       @Param("nextRetryTime") Date nextRetryTime,
                       @Param("version") Integer version);
    
    // 同步处理结果更新
    @Update("UPDATE local_message SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
}